package com.supermarket.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supermarket.entity.Purchase;
import com.supermarket.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
@RequestMapping("purchase")//一级路径
public class PurchaseController {

    @Autowired//自动装配
    private PurchaseService purchaseService;


    @RequestMapping("findPurchaseAll")
    public Map findPurchaseAll(){
        List<Purchase> purchaseList =  purchaseService.findPurchaseAll();

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count", 1000);
        map.put("data",purchaseList);

        return map;

    }
    @RequestMapping("findByPage")
    public Map findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "5") Integer pageSize
    ){

        System.out.println(pageNum);//当前页码
        System.out.println(pageSize);//每页显示条数

        //设置分页参数
        //紧跟PageHelper.startPage()后的第一个查询方法才会被分页
        PageHelper.startPage(pageNum, pageSize);
        List<Purchase> purchaseList = purchaseService.findByPage();

        //封装对象
        PageInfo<Purchase> pageInfo = new PageInfo<>(purchaseList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());//总记录数
        map.put("data",pageInfo.getList());//分页数据

        return map;
    }

    @RequestMapping("findCount")
    public Integer findCount(){
        Integer findCount = purchaseService.findCount();
        return findCount;
    }
    @RequestMapping("add")
    public void add(Purchase purchase, HttpServletResponse response, HttpServletRequest request) throws IOException {
        System.out.println(purchase);

        purchaseService.add(purchase);

        //跳转到主页
        response.sendRedirect(request.getContextPath() + "/home.html");

    }
    @RequestMapping("deleteById")
    public String deleteById(Integer purchaseID){

        //System.out.println(staffID);
        purchaseService.deleteById(purchaseID);

        return "success";
    }
    @RequestMapping("updatePurchase")
    public String updatePurchase(Purchase purchase){
        purchaseService.updatePurchase(purchase);
        //    System.out.println(patient);

        return "success";
    }
    @RequestMapping("deleteByIds")
    public String deleteByIds(Integer [] ids){
        if(ids.length>0){
            purchaseService.deleteByIds(ids);

        }

        return "success";
    }
    @RequestMapping("writeExcel")
    public void writeExcel(Integer [] ids,HttpServletResponse response) throws IOException {
        //System.out.println(Arrays.toString(ids));
        //判断
        if(ids.length > 0){
            //查询要写入的集合对象
            List<Purchase> nurseList = purchaseService.findByIds(ids);
            System.out.println(nurseList);
            //写入Excel
            System.out.println("系统时间：" + System.currentTimeMillis());
            //获取当前时间
            String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            // 写法2
            //String fileName = "D:/patient-" + time + ".xlsx";
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可
            //EasyExcel.write(fileName, Patient.class).sheet("模板").doWrite(patientList);

            //以下载的方式写入Excel
            String fileName = "purchase-" + time + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            //通过流写入
            ServletOutputStream outputStream = response.getOutputStream();
            //注意写入的对象是：outputStream
            EasyExcel.write(outputStream, Purchase.class).sheet("进货标").doWrite(nurseList);
        }
    }
    //layui默认上传对象的变量名是：file
    @RequestMapping("upload")
    public Map upload(MultipartFile file){

        HashMap<String, Object> map = new HashMap<>();

        if(file!=null){

            System.out.println("上传的文件名：" + file.getOriginalFilename());

            purchaseService.save(file);

            map.put("code",0);
            map.put("msg","success");
        }

        return map;
    }
}
